<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Button Sequence Use Autocomplete Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  #area { padding: 10px; }
  #area input { display: block; }
  #subbtn { display: block; margin-top: 5px; }
  #area input, .ui-menu { width: 120px; }
  .bold { font-weight: bold; }
  #dialog { height: auto !important; }
  .ui-dialog { font-size: 10px; }
  .ui-dialog .ui-dialog-titlebar { padding: 2px 3px; height: 15px; }
  .ui-button { outline: 0; }
  .ui-dialog .ui-dialog-content { padding: 0px; margin: 0 5px; }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/ui_utils.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000; // set episode interval to 2000 seconds


var resetUI = function(div){
  if(div.html().length > 0) $('#dialog').dialog('destroy');
  div.empty(); // clear previous problem, if any
}


var createDialog = function(div){
  var html = '<p>' + ui_utils.generateWords(4,8) + '</p>';
  div.append(html);

  $('#dialog').dialog({ height: 70, position: {my: 'center', at: 'center', of: document.getElementById('area')} });

  $('.ui-dialog')[0].style.margin = core.randi(-10,20) + 'px ' + core.randi(5,25) + 'px';
  $('.ui-dialog')[0].style.width = core.randi(90,120) + 'px';
  $('.ui-dialog')[0].style.height = core.randi(70,100) + 'px';
}


var genProblem = function() {
  var task_state = -1.0;
  // click-dialog 
  var dialogDiv = $('#dialog');
  resetUI(dialogDiv);

  var dialogDiv = $('#dialog'); 
  if (dialogDiv.length == 0)  d3.select('#area').append('div').attr('id', 'dialog');
  var dialogDiv = $('#dialog');

  createDialog(dialogDiv);

  // use-autocomplete
  ITEMS = ui_utils.COUNTRIES;
  d3.select('#tags')[0][0].value ='';

  var gtix = core.randi(0, ITEMS.length);
  // the below is complicated because we only want to match beginning of items in list
  $('#tags').autocomplete({
    source: function(request, response) {
      var matcher = new RegExp('^' + $.ui.autocomplete.escapeRegex(request.term), 'i');
      response($.grep(ITEMS, function(item){
          return matcher.test(item);
      }))
    }
  });

  var gtitem = ITEMS[gtix];

  var nf = core.randi(2, 5);
  var f = gtitem.slice(0,nf);

  var ne = core.randi(2, 5);
  var e = gtitem.slice(gtitem.length - ne);

  var match_end = core.randf(0,1) < 0.75;
  if(match_end) {
    // d3.select('#query').html('Enter an item that starts with "<span class="bold">' + f + '</span>" and ends with "<span class="bold">' + e + '</span>", and then close the dialog box by clicking the "x".');
    d3.select('#query').html('Close the dialog box by clicking the "x", after entering an item that starts with "<span class="bold">' + f + '</span>" and ends with "<span class="bold">' + e + '</span>".');
  } else {
    // d3.select('#query').html('Enter an item that starts with "<span class="bold">' + f + '</span>", and then close the dialog box by clicking the "x".');
    d3.select('#query').html('Close the dialog box by clicking the "x", after entering an item that starts with "<span class="bold">' + f + '</span>".');
  }

  d3.select('#subbtn').on('click', function(){
    var entered = d3.select('#tags')[0][0].value;
    var ff = entered.startsWith(f);
    var ee = entered.endsWith(e);
    var correct = ff & (ee || !match_end);
    task_state = correct ? 1.0 : -1.0;
    console.log('use-autocomplete', task_state);
  });

  $('button.ui-button').on('click', function(){
    console.log('click-dialog');
    if (task_state == 1.0) {
      core.endEpisode(1.0, true);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id='dialog'></div>
    <label for="tags">Tags: </label>
    <input id="tags">
    <button id="subbtn" class="secondary-action">Submit</button>
  </div>
</div>
</body>
</html>